#include "key.h"

Key::Key()
{
    Comb = (char *) malloc(8);
    n = 0;
    C =(long long int *) Comb;
    *C = 0;
}

Key::Key(char x)
{
    Comb = (char *) malloc(8);
    n = 1;
    C =(long long int *) Comb;
    *C = 0;
    Comb[0] = x;
}

Key::Key(const Key &K)
{
    Comb = (char *) malloc(8);
    n = K.n;
    C = (long long int *) Comb;
    *C = *K.C;
}

Key::~Key()
{
    free(Comb);
}

Key Key::operator = (Key K)
{
    n = K.n;
    *C = *K.C;
    return *this;
}

Key Key::operator +(Key K)
{
    int i = 0, j = 0;
    Key res;
    while (i != n && j != K.n)
    {
        if (Comb[i] > K.Comb[j])
        {
            res.Comb[res.n++] = K.Comb[j];
            j++;
        }
        else
        {
            res.Comb[res.n++] = Comb[i];
            if (Comb[i] == K.Comb[j])
                j++;
            i++;
        }
        if (res.n == 8)
            return res;
    };
    if (i == n)
        for (; j < K.n && res.n < 8; ++j)
            res.Comb[res.n++] = K.Comb[j];
    else
        for (; i < n && res.n < 8; ++i)
            res.Comb[res.n++] = Comb[i];
    return res;
}

Key Key::operator +=(Key K)
{
    int i = 0, j = 0;
    Key res;
    while (i != n && j != K.n)
    {
        if (Comb[i] > K.Comb[j])
        {
            res.Comb[res.n++] = K.Comb[j];
            j++;
        }
        else
        {
            res.Comb[res.n++] = Comb[i];
            if (Comb[i] == K.Comb[j])
                j++;
            i++;
        }
        if (res.n == 8)
        {
            *this = res;
            return *this;
        }
    };
    if (i == n)
        for (; j < K.n && res.n < 8; ++j)
            res.Comb[res.n++] = K.Comb[j];
    else
        for (; i < n && res.n < 8; ++i)
            res.Comb[res.n++] = Comb[i];
    *this = res;
    return *this;
}

Key Key::operator - (Key K)
{
    int i = 0, j = 0;
    Key res;

    while (i != n && j != K.n)
    {
        if (Comb[i] < K.Comb[j])
        {
            res.Comb[res.n++] = Comb[i];
            i++;
        }
        else
        {
            if (Comb[i] == K.Comb[j])
                i++;
            j++;
        }

    }
    if (i != n)
        for (; i < n && res.n < 8; ++i)
            res.Comb[res.n++] = Comb[i];
    return res;
}

Key Key::operator -= (Key K)
{
    int i = 0, j = 0;
    Key res;

    while (i != n && j != K.n)
    {
        if (Comb[i] < K.Comb[j])
        {
            res.Comb[res.n++] = Comb[i];
            i++;
        }
        else
        {
            if (Comb[i] == K.Comb[j])
                i++;
            j++;
        }
    }
    if (i != n)
        for (; i < n && res.n < 8; ++i)
            res.Comb[res.n++] = Comb[i];
    *this = res;
    return *this;
}

int Key::operator == (Key K)
{
    if (*C == *K.C)
        return 1;
    else
        return 0;
}

int Key::operator != (Key K)
{
    if (*C != *K.C)
        return 1;
    else
        return 0;
}
